<?php
class JobPDF extends TCPDF
    {

        public function Header()
        {
            $this->y = $this->header_margin;
            $this->x = $this->original_lMargin;

            $pagetext = $this->getAliasNumPage() . ' of ' . $this->getAliasNbPages();

            $this->writeHTMLCell(87.8, 20, 106.1, 16, $html = '<h1 style="text-align: center">Job Sales Report</h1>');
            $this->writeHTMLCell(87.8, 20, 106.1, 6, $html = '<h3 style="text-align: center">QSI, INC</h3>');
            $this->SetFontSize(8);
            $this->writeHTMLCell(87.8, 8, 260, 8, $html = $pagetext);

            $this->writeHTMLCell(87.8, 4, 260, 12, $html = date('m-d-Y'));
            $this->writeHTMLCell(87.8, 4, 20, 8, $html = date('m-d-Y'));
            $this->writeHTMLCell(87.8, 4, 20, 12, $html = date('h:i A'));
        }

        /*
         *  @param array of Invoice, Quote, SO
         *  @return string html presentation of object list
         */

        public function parseInvoices($objectList)
        {
            $returnHtml = false;
            foreach ($objectList as $object)
            {
                $amoutPaid = '';
                $remBalance = '';
                $dateExp = explode(' ', $object->invoicedate);
                $date = explode('-', $dateExp[0]);
                $timepstamp = mktime(0, 0, 0, $date[1], $date[2], $date[0]);
                $formattedDate = date('m-d-Y', $timepstamp);
                /* @var $object Invoice */
                switch ($object->invoicetype)
                {
                    case Invoice::TYPE_INVOICE :
                        $type = 'Invoice';
                        $amoutPaid = number_format(round($object->getTotalPaid($object->custno), 2, PHP_ROUND_HALF_UP), 2);
                        $remBalance = number_format(round($object->getBalanceDue(), 2, PHP_ROUND_HALF_UP), 2);
                        break;
                    case Invoice::TYPE_QUOTE :
                        $type = Invoice::TYPE_QUOTE;
                        break;
                    case Invoice::TYPE_SALES_ORDERS :
                        $type = 'Sales Order';
                }
                if ($object->invoicetype != Invoice::TYPE_QUOTE)
                {
                    $ref = $object->quoteorg;
                } else
                    $ref = '';
                $status = $object->getQuoteStatusText();
                switch ($object->invoicetype)
                {

                    case Invoice::TYPE_SALES_ORDERS:
                        $status = JobsStatus::getText($object->salesorderstatus);
                        break;
                    case Invoice::TYPE_QUOTE:
                        $status = $object->getQuoteStatusText();

                        break;
                }
                $returnHtml .= '
          <tr>
              <td aling="left">
                  &nbsp;   ' . $formattedDate . '
              </td>
              <td  aling="left" >
             &nbsp;   ' . $type . '
              </td>
              <td  aling="left">
               &nbsp;   ' . $ref . '
              </td>
                 <td>
              &nbsp;        ' . $object->invoice . '
              </td>
              <td  aling="left">
             &nbsp;       ' . $status . '
              </td>
                <td align="right">
                    ' . number_format(round($object->Total, 2, PHP_ROUND_HALF_UP), 2) . '
              </td>
                <td align="right">
                     ' . $amoutPaid . '
              </td>
                 <td align="right" >
                      ' . $remBalance . '
              </td>


          </tr>
          ';
            }

            return $returnHtml;
        }

        public function parsePayment($invList)
        {
            $returnHtml = '';
            foreach ($invList as $invoice)
            {
                $paymentList = $invoice->getPaymentList();
                foreach ($paymentList as $payment)
                {
                    $dateExp = explode(' ', $payment->entrydate);
                    $date = explode('-', $dateExp[0]);
                    $timepstamp = mktime(0, 0, 0, $date[1], $date[2], $date[0]);
                    $formattedDate = date('m-d-Y', $timepstamp);
                    /* @var $payment RecLed */
                    $returnHtml .= <<<HERE
                          <tr>
                              <td align="left" >
                                 &nbsp; &nbsp; {$formattedDate}
                              </td>
                              <td align="left" >
                               &nbsp;  &nbsp;  {$payment->type}
                              </td>
                              <td align="left" >
                             &nbsp;    &nbsp;  {$payment->notes}
                              </td>
                              <td align="right" >
                              &nbsp;    &nbsp;  {$payment->amount}
                              </td>



                          </tr>
HERE;
                }
            }
            if ($returnHtml)
                return $returnHtml;
            else
                return '<tr><td align="left" colspan="4">&nbsp;&nbsp;None</td></tr>';
        }

    }